package cz.muni.fi.pa165.business.serviceImpl;

import cz.muni.fi.pa165.api.MushroomService;
import cz.muni.fi.pa165.api.dto.MushroomDTO;
import cz.muni.fi.pa165.api.dto.MushroomDTO.Months;
import cz.muni.fi.pa165.api.dto.MushroomDTO.MushroomType;
import cz.muni.fi.pa165.business.dao.MushroomDAO;
import cz.muni.fi.pa165.business.entity.Mushroom;
import cz.muni.fi.pa165.business.service.utils.MushroomObjectTransformer;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Marek Tuhársky
 */
@Service
public class MushroomServiceImpl implements MushroomService {

    private MushroomDAO mushroomDao;

    @Autowired
    public void setMushroomDao(MushroomDAO mushroomDao) {
        this.mushroomDao = mushroomDao;
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    @Transactional
    public void createMushroom(MushroomDTO mushroomDTO) {
        if (mushroomDTO == null) {
            throw new IllegalArgumentException("mushroomDTO is null");
        }
        mushroomDao.createMushroom(MushroomObjectTransformer.mushroomDTOToMushroom(mushroomDTO));
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public MushroomDTO getMushroom(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
        return MushroomObjectTransformer.mushroomToMushroomDTO(mushroomDao.getMushroom(id));

    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Override
    @Transactional
    public void updateMushroom(MushroomDTO mushroomDTO) {
        if (mushroomDTO == null) {
            throw new IllegalArgumentException("mushroomDTO is null");
        }
        mushroomDao.updateMushroom(MushroomObjectTransformer.mushroomDTOToMushroom(mushroomDTO));
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Override
    @Transactional
    public void removeMushroom(MushroomDTO mushroomDTO) {
        if (mushroomDTO == null) {
            throw new IllegalArgumentException("mushroomDTO is null");
        }
        mushroomDao.removeMushroom(MushroomObjectTransformer.mushroomDTOToMushroom(mushroomDTO));
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public List<MushroomDTO> findAllMushrooms() {
        List<MushroomDTO> resultList = new ArrayList<>();
        List<Mushroom> mushroomList = mushroomDao.findAllMushrooms();
        for (Mushroom mushroom : mushroomList) {
            resultList.add(MushroomObjectTransformer.mushroomToMushroomDTO(mushroom));
        }
        return resultList;
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public MushroomDTO findMushroomsByName(String name) {
        if (name == null || name.isEmpty()) {
            throw new IllegalArgumentException("name is null or empty");
        }
        return MushroomObjectTransformer.mushroomToMushroomDTO(mushroomDao.findMushroomsByName(name));
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public List<MushroomDTO> findMushroomsByType(MushroomType type) {
        if (type == null) {
            throw new IllegalArgumentException("type is null");
        }
        List<MushroomDTO> resultList = new ArrayList<>();
        cz.muni.fi.pa165.business.entity.MushroomType mtype = cz.muni.fi.pa165.business.entity.MushroomType.valueOf(type.toString());
        List<Mushroom> mushroomList = mushroomDao.findMushroomsByType(mtype);
        for (Mushroom mushroom : mushroomList) {
            resultList.add(MushroomObjectTransformer.mushroomToMushroomDTO(mushroom));
        }
        return resultList;
    }

    @PreAuthorize("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')")
    @Override
    public List<MushroomDTO> findMushroomsByOccurence(Months from, Months to) {
        if (from == null) {
            throw new IllegalArgumentException("from is null");
        }
        if (to == null) {
            throw new IllegalArgumentException("to is null");
        }
        List<MushroomDTO> resultList = new ArrayList<>();

        cz.muni.fi.pa165.business.entity.Months beginning = cz.muni.fi.pa165.business.entity.Months.valueOf(from.toString());
        cz.muni.fi.pa165.business.entity.Months end = cz.muni.fi.pa165.business.entity.Months.valueOf(to.toString());

        List<Mushroom> mushroomList = mushroomDao.findMushroomsByOccurence(beginning, end);
        for (Mushroom mushroom : mushroomList) {
            resultList.add(MushroomObjectTransformer.mushroomToMushroomDTO(mushroom));
        }
        return resultList;
    }
}
